package com.kedll.library.util;

import android.databinding.BindingAdapter;
import android.databinding.InverseBindingAdapter;
import android.databinding.InverseBindingListener;
import android.databinding.adapters.ListenerUtil;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.kedll.library.R;
import com.kedll.library.widget.AccountItemView;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

import static com.bumptech.glide.load.resource.bitmap.BitmapTransitionOptions.withCrossFade;

//@BindingMethods({
//        @BindingMethod(type = AccountItemView.class, attribute = "acSubTitle", method = "getAcSubTitle")
//})
//
//@InverseBindingMethods({
//        @InverseBindingMethod(type = AccountItemView.class, attribute = "acSubTitle")
//})

/**
 * @author QiQi
 */
public class BindingUtil {


    @BindingAdapter(value = {"imageUrl", "error", "placeholder"}, requireAll = false)
    public static void loadImage(ImageView imgView,
                                 String url,
                                 Drawable error,
                                 Drawable placeholder) {
        GlideApp.with(imgView.getContext())
                .asBitmap()
                .load(url)
                .transition(withCrossFade())
                //缓存策略,跳过内存缓存【此处应该设置为false，否则列表刷新时会闪一下】
                .skipMemoryCache(false)
                //缓存策略,硬盘缓存-仅仅缓存最终的图像，即降低分辨率后的（或者是转换后的）
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                //设置图片加载的优先级
                .priority(Priority.HIGH)
                .error(error)
//                .placeholder(placeholder)
                .centerCrop()
//                .dontAnimate()
                .transform(new RoundedCornersTransformation(MyUtils.dp2px(4), 0))
                .into(imgView);
    }

    @BindingAdapter(value = {"circle", "error"}, requireAll =  false)
    public static void loadCircleImage(ImageView imgView, String url, Drawable error){
        GlideApp.with(imgView.getContext())
                .asBitmap()
                .load(url)
                .centerCrop()
                //缓存策略,跳过内存缓存【此处应该设置为false，否则列表刷新时会闪一下】
                .skipMemoryCache(false)
                //缓存策略,硬盘缓存-仅仅缓存最终的图像，即降低分辨率后的（或者是转换后的）
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                //设置图片加载的优先级
                .priority(Priority.HIGH)
                .error(error)
//                .placeholder(placeholder)
                .dontAnimate()
                .circleCrop()
                .transition(withCrossFade())
                .into(imgView);
    }

    @BindingAdapter(value = {"noTransformUrl", "error", "placeholder"}, requireAll = false)
    public static void loadImageNoTransform(ImageView imgView,
                                            String url,
                                            Drawable error,
                                            Drawable placeholder) {
        GlideApp.with(imgView.getContext())
                .asBitmap()
                .load(url)
                .centerCrop()
                .transition(withCrossFade())
                //缓存策略,跳过内存缓存【此处应该设置为false，否则列表刷新时会闪一下】
                .skipMemoryCache(false)
                //缓存策略,硬盘缓存-仅仅缓存最终的图像，即降低分辨率后的（或者是转换后的）
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                //设置图片加载的优先级
                .priority(Priority.HIGH)
                .error(error)
//                .placeholder(placeholder)
                .dontAnimate()
                .into(imgView);
    }

    @BindingAdapter(value = {"headerUrl", "error", "placeholder"}, requireAll = false)
    public static void loadHeaderImage(ImageView imgView, String headerUrl, Drawable error, Drawable placeHolder) {
        GlideApp.with(imgView.getContext())
                .load(headerUrl)
                .error(error)
                .placeholder(placeHolder)
                .centerCrop()
                .dontAnimate()
                .into(imgView);
    }

    @BindingAdapter(value = {"src"}, requireAll = false)
    public static void setImageResource(ImageView imgView, int resId) {
        if (resId == 0) {
            return;
        }
        imgView.setImageResource(resId);
    }

    @BindingAdapter(value = {"gif"}, requireAll = false)
    public static void setGif(ImageView imgView, int gifResId) {
        GlideApp.with(imgView.getContext())
                .asGif()
                .load(gifResId)
                .placeholder(gifResId)
                .into(imgView);
    }


    @BindingAdapter("android:paddingVertical")
    public static void setPaddingVertical(View view, int paddingVertical) {
        int px = MyUtils.dp2px(paddingVertical);
        view.setPadding(view.getPaddingLeft(), px, view.getPaddingRight(), px);
    }

    @BindingAdapter(value = "textColor")
    public static void setTextColor(TextView view, int colorRes) {
        view.setTextColor(ContextCompat.getColor(view.getContext(), colorRes));
    }


    @InverseBindingAdapter(attribute = "acSubTitle", event = "acSubTitleAttrChanged")
    public static String getAcSubTitle(AccountItemView view) {
        return view.getAcSubTitle();
    }


    @BindingAdapter(value = {"acSubTitleAttrChanged"}, requireAll = false)
    public static void setAcSubTitleTextWatcher(AccountItemView view, final InverseBindingListener acSubTitleAttrChanged) {
        TextWatcher newWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                acSubTitleAttrChanged.onChange();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };

        TextWatcher oldWatcher = ListenerUtil.trackListener(view, newWatcher, R.id.textWatcher);
        if (acSubTitleAttrChanged != null) {
            view.getEditSubTitle().addTextChangedListener(newWatcher);
        }

        if (oldWatcher != null) {
            view.getEditSubTitle().removeTextChangedListener(oldWatcher);
        }
    }

}
